= javascript_include_tag 'mdeditor'
= form_for [:admin, @post], html: {onsubmit: "html_content();"} do |f|
  - if @post.errors.any?
    #error_explanation.alert.alert-danger.alert-dismissible.fade.in
      = button_tag class: 'close', 'data-dismiss' => 'alert', 'aria-label' => 'Close' do
        %span{ 'aria-hidden' => 'true' }
          x
      %h4= "#{@post.errors.count} 个错误导致保存失败："
      %ul
        - @post.errors.full_messages.each do |msg|
          %li= msg
  .form-group
    = f.label :title, '文章标题'
    = f.text_field :title, class: 'form-control', autofocus: true, required: true
  .row
    .col-sm-8
      .form-group
        = f.label :ident, '文章路径'
        .input-group
          .input-group-addon
            = "http://#{Settings.blogine.host}/blog/"
          = f.text_field :ident, class: 'form-control', required: true
    .col-sm-4
      .form-group
        = f.label :column_id, '选择分类'
        = f.select :column_id, Column.all.collect { |c| [ c.name, c.id ] }, {}, class: 'form-control selectpicker', 'data-live-search' => 'true'
  .form-group
    = f.label :intro, '文章摘要'
    = f.text_area :intro, class: 'form-control', placeholder: '对文章内容的简要概述，用于显示在文章列表'
  .form-group
    = f.label :content, '文章内容'
    -# a bug around haml whitespace, I have find some solutions: https://github.com/haml/haml/issues/643#issuecomment-255546050
    = f.text_area :content
    = f.hidden_field :html_content
    = f.hidden_field :status
    = hidden_field_tag :post_tags
.form-group
  %label 选择标签
  = text_field_tag :tags, @tags_str, placeholder: '输入标签，回车确认'
.fixed-area
  .content#posts_content
    .btn-group{'data-toggle': 'buttons'}
      - ::Post::POST_TYPE.each_key do |i|
        %label.btn{class: active_status(i) ? 'active' : ''}
          = radio_button_tag :status, i, active_status(i)
          %i.fa.fa-circle-o
          %i.fa.fa-dot-circle-o
          %span
            = ::Post::POST_TYPE[i]
    = button_tag '提交', id: :submit_form, class: 'btn btn-primary btn-sm'


= javascript_include_tag 'admin/bootstrap-tagsinput.min'
= javascript_include_tag 'admin/bootstrap3-typeahead.min'

:javascript
  window.onbeforeunload = function() {
    return '';
  }
  $("#post_content").markdown({
    language: 'zh',
    imgurl: '/image',
    base64url: '/image_base64',
    fullscreen: {
        enable: true
    },
    autofocus: false,
    resize: 'vertical',
    hiddenButtons: ['cmdEmoji'],
    iconlibrary: 'fa'
  });
  function html_content(){
    $('#post_html_content').val(marked($('#post_content').val()))
  }
  $('#submit_form').click(function(){
    if($('#post_title').val() == '' || $('#post_ident').val() == ''){
      notifyTop('标题或文章路径不能为空！')
    }
    post_status = $('input[name=status]:checked').val()
    $('#post_tags').val($('#tags').val())
    $('#post_status').val(post_status)
    window.onbeforeunload = null;
    $('form').submit()
  })
  $(document).ready(function() {
    $("form input[name*='post']").bind("keypress", function(e) {
      if (e.keyCode == 13) {
          return false;
      }
    });
  });
  var places = #{raw Tag.all.pluck(:name).to_json} ;
  $('#tags').tagsinput({
    typeahead: {
      source: places.map(function(item) { return item }),
      afterSelect: function() {
          this.$element[0].value = '';
      },
      matcher: function(){
        console.log(this.query)
      },
      showHintOnFocus: true,
      scrollHeight: function () {
        var $input = $('.bootstrap-tagsinput > input');
        var $result = $('.typeahead')
        resultHeight = $result.outerHeight() || 220
        prefix = $input.outerHeight() + resultHeight + 10;
        return -prefix
      },
    }
  })
